package com.cst.intercepterdemo.context;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <ul>
 * <li>文件名称 : com.cst.intercepterdemo.context.InterceptContext
 * </li>
 * <li>版权所有 : 版权所有(C)2016-2020</li>
 * <li>公 司 : CST</li>
 * <li>创建时间 : 2016/12/2</li>
 * <li>修改记录 : </li>
 * </ul>
 * 类说明：
 *
 * @author Allen
 * @version 1.0
 */
public class InterceptContext implements IInterceptContext{

    private HttpServletRequest request;

    private String msg;

    private List<String> errorCodes;

    private Map<String,Object> cached;

    public InterceptContext(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void appendMsg(String msg) {

    }

    @Override
    public void addErrorCode(String errorCode) {
        if(errorCodes==null){
            errorCodes = new ArrayList<>();
        }
        errorCodes.add(errorCode);
    }

    @Override
    public String getErrorCode(){
        if(errorCodes==null) {
            return "";
        }
        return Arrays.toString(errorCodes.toArray());
    }

    @Override
    public void cache(String key, Object value){
        if(cached==null){
            cached = new HashMap<>();
        }
        cached.put(key,value);
    }

    @Override
    public Object getCache(String key){
        if(cached==null){
            return null;
        }
        return cached.get(key);
    }

    @Override
    public HttpServletRequest getRequest() {
        return request;
    }
}
